$(function () {
    $("#content").keyup(function () {
        var len = $(this).val().length;
        if (len > 299) {
            $(this).val($(this).val().substring(0, 300));
        }
        $("#word").text(len);
    });
    var id=$('#id').val()
    initFileInput('file-Portrait','/user/uploadFile')
    // function initFileInput(ctrlName, uploadUrl) {
    //   var control = $('#' + ctrlName);
    //   console.log(control);
    //   control.fileinput({
    //     language: 'zh', //设置语言
    //     uploadUrl: uploadUrl, //上传的地址
    //     allowedFileExtensions: ['jpg', 'png', 'gif'], //接收的文件后缀
    //     showUpload: false, //是否显示上传按钮
    //     showPreview :false,
    //     dropZoneEnabled: true,
    //     showCaption: false, //是否显示标题
    //     browseClass: "btn btn-primary", //按钮样式
    //     previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
    //   });
    // }
    // $("#inputimg").click(function(){
    //   initFileInput("file-Portrait", "/User/EditPortrait")
    // })

})
//修改信息
$('.validateForm').off().submit(function () {
    // var data=$('.validateForm').serialize();
    // var data =new formdata()
    var data = new FormData()
    data.append('id', $('#id').val())
    data.append('username', $('#username').val())
    data.append('school',  $('#school').val())
    data.append('content', $('#content').val())
    data.append('education', $('#education').val())
    console.log($('#education').val())
    // var data=$('.validateForm').serialize()
    console.log(data)
    $.ajax({
            url: '/user/updateUser',
            type: 'POST',
            dataType: "json",
            data:data,
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.data,
                            type: 'success',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        window.setInterval(function () {
                            this.history.go(0)
                        },1500)


                    } else {
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'error',
                            delay: 30000,
                            pause_on_hover: false
                        });
                    }
                }
        }
    )
    return false;
})
// 上传图片
function initFileInput(ctrlName, uploadUrl){
    var control = $('#' + ctrlName);
    control.fileinput({
        language: 'zh', //设置语言
        allowedFileExtensions: ['jpg', 'gif', 'png'], //接收的文件后缀
        uploadAsync: true, //默认异步上传
        showUpload: true, //是否显示上传按钮
        uploadLabel: "修改头像",
        uploadUrl: uploadUrl,
        uploadIcon:"<i class='fa-upload fas'></i>",
        removeIcon:"<i class='fa-trash fas'></i>",
        removeLabel:"移除照片",

        uploadExtraData:function(){
            var id = {
                'id':$('#id').val()
            }
            return id;
        },
        showRemove: true, //显示移除按钮

        showPreview: true, //是否显示预览

        showCaption: false, //是否显示标题
        browseClass: "btn btn-primary", //按钮样式   
        browseLabel: "选择图片",
        fileActionSettings:{
            removeTitle:'删除文件',
            zoomTitle:'查看详情',
            uploadTitle:'上传文件',
            uploadClass:'btn btn-xs btn-default',
            uploadIcon: '<i class=\'fa-upload fas\'></i>',
            removeClass:'btn btn-xs btn-default',
            removeIcon: '<i class=\'fa-trash fas\'></i>',
            zoomClass:'btn btn-xs btn-default',
            zoomIcon: '<i class=\'fa-search-plus fas\'></i>',
            uploadRetryTitle:'重新上传',
            uploadRetryClass:'btn btn-xs btn-default',
            uploadRetryIcon: '<i class=\'fa-upload fas\'></i>'
        },
        // previewZoomButtonClasses: {prev:'btn btn-navigate',
        // next:'btn btn-navigate',
        // toggleheader:'btn btn-default btn-header-toggle',
        // fullscreen:'btn- btn-default',
        // borderless:'btn btn-default',
        // close:'btn btn-default'},

        dropZoneEnabled: true, //是否显示拖拽区域

        //minImageWidth: 50, //图片的最小宽度

        //minImageHeight: 50,//图片的最小高度

        // maxImageWidth: 50,//图片的最大宽度

        // maxImageHeight: 50,//图片的最大高度

        maxFileSize: 2000, //单位为kb，如果为0表示不限制文件大小

        // minFileCount: 2,

        maxFileCount: 1, //表示允许同时上传的最大文件个数

        enctype: 'multipart/form-data',

        validateInitialCount: true,
        dropZoneTitle: '拖拽图片到这里',

        // previewFileIcon: "<i class='fa-bell fas'></i>",

        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
    }).on("fileuploaded", function (event, data, previewId, index) {
        if(data.response.code==200){
            $.toast({
                title: '通知',
                subtitle: '1秒前',
                content: data.response.data,
                type: 'success',
                delay: 3000,
                pause_on_hover: false
            });
            window.setInterval(function () {
               this.history.go(0)
            },1500)


        }else{
            $.toast({
                title: '通知',
                subtitle: '5秒前',
                content: data.response.msg,
                type: 'error',
                delay: 30000,
                pause_on_hover: false
            });
        }

    }).on('fileuploaderror', function(event, data, previewId, index){
        //该方法将在上传出現異常触发
        $.toast({
            title: '通知',
            subtitle: '5秒前',
            content: "上传失败",
            type: 'error',
            delay: 30000,
            pause_on_hover: false
        });
    })

}
